/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.common.persistence;


import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Map.Entry;

/**
 * Created by Peter.Li on 2015/10/9.
 */
public class SearchFilter {

    /**
     * Enum to Operators.
     */
    public enum Operator {
        EQ, LIKE, GT, LT, GTE, LTE
    }

    private String fieldName;
    private Object value;
    private Operator operator;

    /**
     * Constructor.
     *
     * @param fieldName String
     * @param operator  Operator
     * @param value     Object
     */
    public SearchFilter(String fieldName, Operator operator, Object value) {
        this.fieldName = fieldName;
        this.value = value;
        this.operator = operator;
    }

    /**
     * Format the key like : OPERATOR_FIELDNAME
     *
     * @param searchParams Map<String, Object>
     * @return Map<String, SearchFilter>
     */
    public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = Maps.newHashMap();

        for (Entry<String, Object> entry : searchParams.entrySet()) {
            // filter the empty value
            String key = entry.getKey();
            Object value = entry.getValue();
            if (StringUtils.isBlank((String) value)) {
                continue;
            }

            // split the operator and filedAttribute
            String[] names = StringUtils.split(key, "_");
            if (names.length != 2) {
                throw new IllegalArgumentException(key + " is not a valid search filter name");
            }
            String filedName = names[1];
            Operator operator = Operator.valueOf(names[0]);

            // new searchFilter
            SearchFilter filter = new SearchFilter(filedName, operator, value);
            filters.put(key, filter);
        }

        return filters;
    }

    /**
     * Remove the prefix of parameters.
     *
     * @param searchParams Map<String, Object>
     * @return the new Map with no prefix parameters.
     */
    public static Map<String, Object> parseParam(Map<String, Object> searchParams) {
        Map<String, Object> filters = Maps.newHashMap();

        for (Entry<String, Object> entry : searchParams.entrySet()) {
            // filter the empty value
            String key = entry.getKey();
            Object value = entry.getValue();
            if (StringUtils.isBlank((String) value)) {
                continue;
            }

            // split the operator and filedAttribute
            String[] names = StringUtils.split(key, "_");
            if (names.length == 2) {
                String filedName = names[1];
                Operator operator = Operator.valueOf(names[0]);

                // new searchFilter
                //SearchFilter filter = new SearchFilter(filedName, operator, value);
                filters.put(filedName, value);
            }
        }
        return filters;
    }
}


